1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4 using
UnityEngine.UI;
5 using
UnityEngine.SceneManagement;
6
7 public
class Pin : MonoBehaviour {
8
9     
public float speed;
10     
public Rigidbody2D myRigidBody;
11     
public RuntimeAnimatorController cameraZoom;
12     
public bool isPrepareToShoot = false;
13     
public bool hasMove = false;
14     
public AudioClip hit;
15
16     
private AudioSource audioSource;
17     
private Animator animator;
18     
private bool isSticked = false;
19     
private bool isFire = false;
20     
private bool hasTouched = false;
21
22     
void Awake(){
23         audioSource = GetComponent<AudioSource> ();
24         animator = GameObject.FindGameObjectWithTag (
"MainCamera").transform.GetComponent<Animator> ();
25     }
26
27     
// Use this for initialization
28     
void Start () {
29         
30     }
31     
32     
// Update is called once per frame
33     
void Update () {
34         
if(Application.platform == RuntimePlatform.WindowsEditor){
35             KeyBoardButton ();
36         }
else if(Application.platform == RuntimePlatform.Android){
37             TouchButton ();
38         }
39             
40         MoveUpward ();
41     }
42
43
44     
void KeyBoardButton(){
45         
if(Input.GetKeyDown(KeyCode.Space)){
46             
if(isPrepareToShoot){
47                 isFire =
true;
48                 hasMove =
true;
49             }
50         }
51     }
52
53     
void TouchButton(){
54         
if(Input.touchCount > 0){
55             Touch touch = Input.GetTouch (
0);
56
57             
if(touch.phase == TouchPhase.Began){
58                 
if(isPrepareToShoot){
59                     isFire =
true;
60                     hasMove =
true;
61                 }
62             }
63         }
64     }
65
66     
void MoveUpward(){
67         
if (!isSticked && isPrepareToShoot && isFire) {
68             myRigidBody.MovePosition (myRigidBody.position + Vector2.up * speed * Time.deltaTime);
69             transform.GetChild (
0).gameObject.SetActive (true);
70         }
71     }
72
73     
void OnTriggerEnter2D(Collider2D collider){
74         
if (collider.CompareTag ("Target")) {
75             isSticked =
true;
76             
if(isSticked && !hasTouched){
77                 audioSource.PlayOneShot (hit);
78                 GameplayController.instance.score++;
79                 
if(GameplayController.instance.score == GameObject.FindGameObjectWithTag("Spawner").transform.GetComponent<Spawner>().totalPin){
80                     GameObject.Find (
"Background").transform.GetComponent<Image> ().color = Color.green;
81                     GameObject.FindGameObjectWithTag (
"Target").transform.GetComponent<Target> ().enabled = false;
82                     Vector3 temp = transform.GetChild (
0).transform.localScale;
83                     temp =
new Vector3 (temp.x, temp.y * temp.y, temp.z);
84                     GameObject[] pin = GameObject.FindGameObjectsWithTag (
"Pin");
85                     
foreach(GameObject newPin in pin){
86                         newPin.transform.GetChild (
0).transform.localScale = temp;
87                         newPin.transform.GetChild (
1).transform.GetChild (0).transform.gameObject.SetActive (false);
88                         newPin.transform.GetComponent<SpriteRenderer> ().enabled =
false;
89                     }
90                     StartCoroutine (NextLevel ());
91                     isPrepareToShoot =
false;
92                     isFire =
false;
93                 }
94             }
95
96             GameObject.FindGameObjectWithTag (
"Spawner").transform.GetComponent<Spawner> ().IsPrepareToFire ();
97             transform.SetParent (collider.transform);
98
99         }
else if(collider.CompareTag("Pin")) {
100             
if (!isSticked) {
101                 hasTouched =
true;
102                 GameObject.FindGameObjectWithTag (
"Target").transform.GetComponent<Target> ().enabled = false;
103                 GameObject.Find (
"Background").transform.GetComponent<Image> ().color = Color.red;
104                 StartCoroutine (RestartLevel ());
105                 animator.runtimeAnimatorController = cameraZoom;
106                 isPrepareToShoot =
false;
107                 isFire =
false;
108             }
109         }
110     }
111
112     IEnumerator NextLevel(){
113         
yield return new WaitForSeconds (1f);
114         SceneManager.LoadScene (
"Success");
115     }
116
117     IEnumerator RestartLevel(){
118         
yield return new WaitForSeconds (1f);
119         SceneManager.LoadScene (
"Failed");
120     }
121
122
123 }


Gõ tìm kiếm nhanh...